package com.ruoyi.web.controller.authentication;


import cn.hxzy.common.CertificationStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.authentication.domain.TsVehicleCertification;
import com.hxzy.authentication.service.TsVehicleCertificationService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mingqian.Wu
 * @since 2023-06-23
 */
@RestController
@RequestMapping("/tsVehicleCertification")
@DataSource(DataSourceType.AUTHENTICATION)
public class TsVehicleCertificationController {

    @Autowired
    private TsVehicleCertificationService tsVehicleCertificationService;
    @Value("${oss-img-path}")
    private String ossImgPath;
    /**
     * 查询车辆认证列表
     */
    @Anonymous
    @GetMapping("/list/{pageIndex}/{pageSize}")
    public AjaxResult list(@PathVariable Integer pageIndex, @PathVariable Integer pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<TsVehicleCertification> list = tsVehicleCertificationService.list();
        PageInfo pageInfo=new PageInfo(list);
        long total = pageInfo.getTotal();
        return AjaxResult.success(pageInfo.getList()).put("total",total);
    }


    //根据ID获取详情
    @Anonymous
    @GetMapping("/getById")
    public AjaxResult getById(@RequestParam String authenticationId)
    {
        TsVehicleCertification byId = tsVehicleCertificationService.getById(authenticationId);
//byId.getEnergySign(ossImgPath+byId.getCertificationStatus(byId))
        return AjaxResult.success(byId);
    }

    //审批完成后修改认证状态
    @Anonymous
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestParam int certificationStatus,@RequestBody TsVehicleCertification tsVehicleCertification) {
        QueryWrapper<TsVehicleCertification> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("certification_status",certificationStatus);
        TsVehicleCertification tsVehicleCertification1 = tsVehicleCertification.setCertificationStatus(queryWrapper.getEntity().getCertificationStatus());
        boolean update = tsVehicleCertificationService.update(tsVehicleCertification1, queryWrapper);
        if (update==true){
            return AjaxResult.success(tsVehicleCertification1);
        }
        return AjaxResult.error();
    }

    //搜索框
    @Anonymous
    @PostMapping("/getTs/{pageIndex}/{pageSize}")
    public AjaxResult getTs(@RequestBody TsVehicleCertification tsVehicleCertification,@PathVariable Integer pageIndex,@PathVariable Integer pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<TsVehicleCertification> ts = tsVehicleCertificationService.getTs(tsVehicleCertification);
        return AjaxResult.success(ts);
    }



    //修改认证状态
    @Anonymous
    @PostMapping("/getZhuangTai")
    public AjaxResult setZhuangTai(@RequestParam("certificationStatus") int certificationStatus){
        QueryWrapper<TsVehicleCertification>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("certification_status",certificationStatus);
        List<TsVehicleCertification> list = tsVehicleCertificationService.list(queryWrapper);
        list.stream().forEach(item->{
            item.setRenzheng(CertificationStatus.getCertificationStatusValue(item.getCertificationStatus()));
        });
        return AjaxResult.success(list);
    }


    //根据认证状态查询列表
    @Anonymous
    @GetMapping("/getZhuangTai/{pageIndex}/{pageSize}")
    public AjaxResult getZhuangTai(@RequestParam("certificationStatus") int certificationStatus,@PathVariable Integer pageIndex,@PathVariable Integer pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<TsVehicleCertification> zhuangTai = tsVehicleCertificationService.getZhuangTai(certificationStatus);
        return AjaxResult.success(zhuangTai);
    }


}

